package com.smallA.service.Impl;

import com.smallA.constant.DataConstant;
import com.smallA.mapper.ExamMapper;
import com.smallA.mapper.ExamQuestionMapper;
import com.smallA.po.Choice;
import com.smallA.po.ExamQuestion;
import com.smallA.po.Fill;
import com.smallA.po.ShortAnswer;
import com.smallA.service.ExamQuestionService;
import com.smallA.service.ExamService;
import com.smallA.utils.DataUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 试卷试题中间类
 * @Author leon
 * @create 2021/8/16 18:08
 */
@Service("ExamQuestionService")
public class ExamQuestionServiceImpl implements ExamQuestionService {
    @Resource
    private DataUtils dataUtils;
    @Resource
    private ExamQuestionMapper examQuestionMapper;
    @Resource
    private ExamService examService;
    @Resource
    private ExamMapper examMapper;

    @Override
    public Map<String, Object> selectExamById(int examId) {
        if (dataUtils.examineNumber(examId)) {
            return null;
        }
        List<ExamQuestion> lists = examQuestionMapper.selectExamById(examId);
        List<Choice> choiceList = new ArrayList<>();
        List<Fill> fillList = new ArrayList<>();
        List<ShortAnswer> shortAnswerList = new ArrayList<>();
        Map<String, Object> map = new HashMap<>();
        for (ExamQuestion list : lists) {
            int id = list.getQuestionId();
            if (list.getQuestionType() == 1) {
                //选择
                choiceList.add(examQuestionMapper.selectChoiceById(id));
            } else if (list.getQuestionType() == 2) {
                //填空
                fillList.add(examQuestionMapper.selectFillById(id));
            } else {
                //简答
                shortAnswerList.add(examQuestionMapper.selectShortById(id));
            }
        }
        map.put(DataConstant.QUESTION_CHOICE, choiceList);
        map.put(DataConstant.QUESTION_FILL, fillList);
        map.put(DataConstant.QUESTION_SHORT_ANSWER, shortAnswerList);
        return map;
    }

    @Override
    public Map<String, Object> selectAllQuestion() {
            List<Choice> choiceList = examQuestionMapper.selectAllChoice();
            List<Fill> fillList = examQuestionMapper.selectAllFill();
            List<ShortAnswer> shortAnswerList = examQuestionMapper.selectAllShort();


            Map<String, Object> map = new HashMap<>();

            map.put(DataConstant.QUESTION_CHOICE, choiceList);
            map.put(DataConstant.QUESTION_FILL, fillList);
            map.put(DataConstant.QUESTION_SHORT_ANSWER, shortAnswerList);
            return map;
    }

    @Override
    public Object addExam(int tid, String analyze, Integer[] choices, Integer[] fills, Integer[] shortAnswers, Integer score) {
        if (dataUtils.examineNumber(tid,score)|| (dataUtils.examineNumArray(choices)&&dataUtils.examineNumArray(fills)&& dataUtils.examineNumArray(shortAnswers))) {
            return null;
        }
        if(dataUtils.examineStr(analyze,100)){
            return null;
        }
        examMapper.addExam(tid,analyze,score);
        Integer eid = examMapper.selectExamByAnalyze(analyze);
        //题目的总数目
        if(choices==null){
            choices = new Integer[]{};
        }
        if(fills==null){
            fills = new Integer[]{};
        }
        if(shortAnswers==null){
            shortAnswers = new Integer[]{};
        }
        Integer number = choices.length+fills.length+shortAnswers.length;
        Float scores = (float)(Math.round((score*1.0/number)*100)/100);
        for (int i = 0; i < choices.length; i++) {
            examQuestionMapper.addQuestionInExam(eid, Integer.valueOf(choices[i]),DataConstant.CHOICE_TYPE,scores);
        }
        for (int i = 0; i < fills.length; i++) {
            examQuestionMapper.addQuestionInExam(eid, Integer.valueOf(fills[i]),DataConstant.FILL_TYPE,scores);
        }
        for (int i = 0; i < shortAnswers.length; i++) {
            examQuestionMapper.addQuestionInExam(eid, Integer.valueOf(shortAnswers[i]),DataConstant.SHORT_TYPE,scores);
        }
        Map<String, Object> map = selectExamById(eid);
        List<Choice> choiceList = (List<Choice>) map.get(DataConstant.QUESTION_CHOICE);
        List<Fill> fillList = (List<Fill>) map.get(DataConstant.QUESTION_FILL);
        List<ShortAnswer> shortList = (List<ShortAnswer>) map.get(DataConstant.QUESTION_SHORT_ANSWER);
        List<Float> list = new ArrayList<>();
        Float difficulty = Float.valueOf(0);
        int calculate = 0;
        for (int i = 0; i < choiceList.size(); i++) {
            list.add(choiceList.get(i).getDifficulty());
        }
        for (int i = 0; i < fillList.size(); i++) {
            list.add(fillList.get(i).getDifficulty());
        }
        for (int i = 0; i < shortList.size(); i++) {
            list.add(shortList.get(i).getDifficulty());
        }
        for (int i = 0; i < list.size(); i++) {
            difficulty += list.get(i);
            ++calculate;
        }
        examMapper.updateDifficulty(difficulty/calculate,eid);
        return examService.selectExamByExamId(eid);
    }
}
